$('.datatable-basic').DataTable({
	 "paging": false, 
	 "ajax" : {
		"url" : contextpath + "cOrderPayment/list",
		"data" : function(d) {
            var query = $("#query").serializeObject();
            query["size"] = d.length;
            query["page"] = (d.start/d.length);
			return query;
		},
		"dataFilter": function(data){
            var json = jQuery.parseJSON( data );
            json.recordsTotal = json.totalElements;
            json.recordsFiltered = json.totalElements;
            json.data = json.content;
 
            return JSON.stringify( json );
        }
	 },
	 "oLanguage" : {  
         "sInfo" : ""
     },
	 "columns": [
        /* {
        	 render : function(data, type, row, meta) {
        		 if(row.payType == '0'){
        			 return '千郡pos机一号机';
        		 }
        		 if(row.payType == '1'){
        			 return '千郡pos机二号机';
        		 }
        		 if(row.payType == '2'){
        			 return '微信';
        		 }
        		 if(row.payType == '3'){
        			 return '支付宝';
        		 }
        		 if(row.payType == '4'){
        			 return '收钱吧';
        		 }
        		 if(row.payType == '5'){
        			 return '现金';
        		 }
        		 if(row.payType == '6'){
        			 return '店收';
        		 }
        		 if(row.payType == '7'){
        			 return '转账（招行9980）';
        		 }
        		 if(row.payType == '8'){
        			 return '千郡pos机四号机';
        		 }
        		 if(row.payType == '9'){
        			 return '欧曼国际pos机';
        		 }
        		 if(row.payType == '10'){
        			 return '转账（中行8883）';
        		 }
        		 if(row.payType == '11'){
        			 return '梁总个人微信';
        		 }
        		 if(row.payType == '12'){
        			 return '梁总个人支付宝';
        		 }
        		 if(row.payType == '13'){
        			 return '分期G';
        		 }
        		 if(row.payType == '14'){
        			 return '分期S';
        		 }
        		 if(row.payType == '15'){
        			 return '店内收款';
        		 }
        		 if(row.payType == '8'){
        			 return '千郡pos机四号机';
        		 }
        		 return '';
        	 }
         },*/
         {
        	 render : function(data, type, row, meta) {
        		 if(row.payee == '0'){
        			 return '医院';
        		 }
        		 if(row.payee == '1'){
        			 return '店家';
        		 }
        		 return '';
        	 }
         },
         { "data": "desc" },
         { "data": "feeAmount" },
         { "data": "amount" },
         {
        	 render : function(data, type, row, meta) {
        		 if(row.type == '0'){
        			 return '定金';
        		 }
        		 if(row.type == '1'){
        			 return '尾款';
        		 }
        		 if(row.type == '2'){
        			 return '预付款';
        		 }
        		 if(row.type == '3'){
        			 return '全款';
        		 }
        		 if(row.type == '4'){
        			 return '其他';
        		 }
        		 return '';
        	 }
         },
         { "data": "payTime" },
         {
             render : function(data, type, row, meta) {
            	 if(row.isBalance == '1'){
            		 return $("#btnGroup").html();
            	 }else{
            		 return '';
            	 }
             }
         }
     ],
     "drawCallback": function( settings ) {
    	 $(".btn-edit").click(function(){
    		var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
    		$("#edit").validate().resetForm();
 		    $("#edit").fill(row);
 		    
 		    $(".select").trigger("change");
 		 });
    	 $(".btn-balance").click(function(){
    		 var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
 			swal({
 				title : "结算该收款记录",
 				text : "结算该收款?",
 				type : "warning",
 				showCancelButton : true,
 				confirmButtonColor : "#DD6B55",
 				confirmButtonText : "确定",
 				cancelButtonText: "取消",
 				closeOnConfirm : false
 			},function() {
 				$.get(contextpath + "cOrderPayment/balance/" + row.id, function(d){
 					$('.datatable-basic').DataTable().draw();
 					swal("结算该收款记录成功","", "success");
 				})
 			});
    	 });
         $(".btn-del").click(function(){
             var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
             swal({
                 title : "删除",
                 text : "确定要删除此收款明细?",
                 type : "warning",
                 showCancelButton : true,
                 confirmButtonColor : "#DD6B55",
                 confirmButtonText : "确定",
                 cancelButtonText: "取消",
                 closeOnConfirm : false
             },function() {
                 $.get(contextpath + "cOrderPayment/delete/" + row.id, function(d){
                     $('.datatable-basic').DataTable().draw();
                     swal("删除收款明细成功","", "success");
                 })
             });
         });
      }
});

$(function(){
	
	$("#submitPayment").click(function(){
		if($("#edit").valid()){
			if($("#payType").val() == '-1'){
				swal("请选择付款方式","", "error");
				return;
			}
			
			if($("#payee").val() == '-1'){
				swal("请选择收款方","", "error");
				return;
			}

			if($("#type").val() == '-1'){
				swal("请选择金额性质","", "error");
				return;
			}
			
			$.post(contextpath + "cOrderPayment/save", $("#edit").serialize(), function(){
				swal("保存收款明细成功","", "success");
				$('.datatable-basic').DataTable().draw();
				$('.close').click();
			})
		}
	});
	
	$("#submit").click(function(){
		$('.datatable-basic').DataTable().draw();
	});
	
	 $('#payTime').daterangepicker(
			  { 
				  "singleDatePicker": true,
				  "parentEl":$("#edit_dialog"),
				  showDropdowns: true,
			    locale:{
			    	format: 'YYYY-MM-DD',
		            applyLabel: '确认',
		            cancelLabel: '取消',
		            fromLabel: '从',
		            toLabel: '到',
		            customRangeLabel: '选择时间',
		            daysOfWeek:["日","一","二","三","四","五","六"],
		            monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
		        }
			  }
			);
	 $('#payTime').val("");

	 $('select').select2({
		    minimumResultsForSearch: Infinity
		});	
	 
	 $(".btn-add").click(function(){
			$("#edit").validate().resetForm();
			$("#edit")[0].reset();
			$(".select").val("0");
		 	$(".select").trigger("change");
		});
	 
	 $('#payType').val("3").trigger('change');
	 
})
